package com.tomorrow.runner.service;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Build;

import com.tomorrow.runner.util.AndroidBuild;
import com.tomorrow.runner.util.SensorUtil;

/**
 * Created by tomorrow on 16-5-7.
 */
public class PushUpDetector {
    public interface OnPushUpDetectListener{
        void onPushUpDetect();
    }

    private OnPushUpDetectListener mOnPushUpDetectListener;
    private Context mContext;
    private SensorManager manager;
    public PushUpDetector(Context context,OnPushUpDetectListener listener){
        this.mOnPushUpDetectListener = listener;
        mContext = context;
        if (!SensorUtil.isSupportSensor(context, Sensor.TYPE_PROXIMITY)){
            throw  new IllegalStateException("you device do not support TYPE_PROXIMITY sensor");
        }
        manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    }

    public boolean startDetect(){
        if (mOnPushUpDetectListener == null){
            throw new IllegalStateException("you should set an OnStepDetectListener before detect");
        }
        Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        if (AndroidBuild.isGreatThanOrEqualSdk(Build.VERSION_CODES.KITKAT)){
            return manager.registerListener(sensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL,1000);
        }
        else{
            return manager.registerListener(sensorListener,Sensor.TYPE_PROXIMITY,SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    public void stopDetect(){
        if (AndroidBuild.isGreatThanOrEqualSdk(Build.VERSION_CODES.KITKAT)){
            manager.unregisterListener(sensorEventListener);
        }
        else{
            manager.unregisterListener(sensorListener);
        }
    }
    private boolean init = false;
    private long lastInitTime = 0 ;
    private void detectPushUp(int sensorType,float value){
        Sensor sensor = manager.getDefaultSensor(sensorType);
        if (value < sensor.getMaximumRange()){//接近
            if (!init){
                return;
            }
            mOnPushUpDetectListener.onPushUpDetect();
        }
        else{
            if (System.currentTimeMillis() - lastInitTime > 1000){
                lastInitTime = System.currentTimeMillis();
                init = true;
            }
        }
    }
    //SDK >= KITKAT
    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            detectPushUp(event.sensor.getType(),event.values[0]);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    //SDK < KITKAT
    private SensorListener sensorListener = new SensorListener() {
        @Override
        public void onSensorChanged(int sensor, float[] values) {
            detectPushUp(sensor,values[0]);
        }

        @Override
        public void onAccuracyChanged(int sensor, int accuracy) {

        }
    };
}
